package neusoft.edu.cn.a215lmeordering.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.LocationClient;

import neusoft.edu.cn.a215lmeordering.Listener.Listener;
import neusoft.edu.cn.a215lmeordering.Listener.MyLocationListener;
import neusoft.edu.cn.a215lmeordering.R;
import neusoft.edu.cn.a215lmeordering.activities.LoginActivity;
import neusoft.edu.cn.a215lmeordering.activities.UpdateUserActivity;
import neusoft.edu.cn.a215lmeordering.activities.SetUpActivity;
import neusoft.edu.cn.a215lmeordering.bean.Person;
import neusoft.edu.cn.a215lmeordering.iface.LocationIface;
import neusoft.edu.cn.a215lmeordering.model.LocationModel;
import neusoft.edu.cn.a215lmeordering.model.UserModel;


public class UserFragment extends Fragment {

    EditText show;
    Button sreach;
    String addr;
    private ImageView SetUp,Person_xinxi;
    private TextView name,phone;

    public LocationClient mLocationClient = null;
    private MyLocationListener myListener = new MyLocationListener();

    private LocationIface locationIface = new LocationIface() {
        @Override
        public void getResultList(String address) {
            addr=address;
            if(!show.getText().toString().equals(addr)){
                Toast.makeText(getContext(), "已获取当前位置，请复制...", Toast.LENGTH_SHORT).show();
            }
            show.setText(addr);
        }
    };

    private Listener listener=new Listener() {
        @Override
        public void onResponse(Object o) {
            Person person = (Person) o;
           name.setText(person.getUsername());
           phone.setText(person.getMobilenum());
        }

        @Override
        public void onFail(String msg) {

        }
    };

    public UserFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_user, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        location(view);

        init(view);

        set(view);

        SetUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), SetUpActivity.class);
                startActivity(intent);
            }
        });

        Person_xinxi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), UpdateUserActivity.class);
                startActivity(intent);
            }
        });

    }

    private void set(View view) {
        name=view.findViewById(R.id.person_userName);
        phone=view.findViewById(R.id.person_phone);

        UserModel userModel = new UserModel();
        userModel.getResultList(Integer.parseInt(LoginActivity.UserId),listener);
    }

    private void init(View view) {
        SetUp=view.findViewById(R.id.person_shezhi);
        Person_xinxi=view.findViewById(R.id.person_xinxi);
    }

    private void location(View view) {
        show = view.findViewById(R.id.show);
        sreach = view.findViewById(R.id.search);

        sreach.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //百度地图定位初始化
                mLocationClient = new LocationClient(getContext());//声明LocationClient类
                mLocationClient.registerLocationListener(myListener);//注册监听函数

                LocationModel locationModel = new LocationModel();
                locationModel.startLocate(mLocationClient);

                myListener.setListener(locationIface);

                Toast.makeText(getContext(), "正在查询,请稍后...", Toast.LENGTH_SHORT).show();

            }
        });
    }

}
